package com.nhjf.service.impl.coursesSeries;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nhjf.dao.coursePicture.CoursePictureDao;
import com.nhjf.dao.coursesSeries.CoursesSeriesDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.coursePicture.CoursePicture;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.coursesSeries.CoursesSeriesService;
import com.nhjf.service.impl.BaseServiceImpl;
@Transactional
@Service("coursesSeriesService")
public class CoursesSeriesServiceImpl extends BaseServiceImpl<CoursesSeries, CoursesSeriesQo, CoursesSeriesDao> implements CoursesSeriesService{

	@Autowired
	private CoursesSeriesDao coursesSeriesDao;
	@Autowired
	private TeachersDao teacherDao;
	@Autowired
	private CoursePictureDao coursePictureDao;
	@Override
	protected CoursesSeriesDao getDao() {
		// TODO Auto-generated method stub
		return coursesSeriesDao;
	}
	@Override
	public ServiceResultDto findadd(CoursesSeriesQo qo) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		CoursesSeries coursesSeries=new CoursesSeries();
		coursesSeries.setIsRelease(1);
		if(qo==null){
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		//课程标题
		if(qo.getTotalTitle()!=null){
			coursesSeries.setTotalTitle(qo.getTotalTitle());
		}else {
			resultDto.setResultMessage("课程标题为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		//是否置顶
		if(qo.getIsTopping()!=null) {
			coursesSeries.setIsTopping(qo.getIsTopping());
		}
		//二级课程标题
		if(qo.getCourseSecondaryTitle()!=null){
			coursesSeries.setCourseSecondaryTitle(qo.getCourseSecondaryTitle());
		}
		//封面
		if(qo.getCover()!=null){
			coursesSeries.setCover(qo.getCover());
		}else{
			resultDto.setResultMessage("封面为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		//是否付费
		if(qo.getIsPrice()==1 || qo.getIsPrice()==2){
			coursesSeries.setIsPrice(qo.getIsPrice());
//			if(qo.getIsPrice()==2) {
//				if(qo.getPrice()!=null) {
//					coursesSeries.setPrice(qo.getPrice());
//				}else {
//					resultDto.setResultMessage("请输入费用！");
//					resultDto.setSuccess(false);
//					return resultDto;
//				}

//			}
		}

		//一级分类
		if(qo.getCourseTypeOne()!=null){
			coursesSeries.setCourseTypeOne(qo.getCourseTypeOne());
		}
		//二级分类
		if(qo.getCourseTypeTwo()!=null){
			coursesSeries.setCourseTypeTwo(qo.getCourseTypeTwo());
		}
		//课程共享
		if(qo.getCourseShare()==1||qo.getCourseShare()==2||qo.getCourseShare()==3){
			coursesSeries.setCourseShare(qo.getCourseShare());
		}
		//课程简介
		if(qo.getTotalDetails()!=null){
			coursesSeries.setTotalDetails(qo.getTotalDetails());
		}else{
			resultDto.setResultMessage("课程简介为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(StringUtils.isNotBlank(qo.getTotalLength())) {
			coursesSeries.setTotalLength(qo.getTotalLength());
		}
		if(qo.getIsReview()!=null) {
			coursesSeries.setIsReview(qo.getIsReview());
		}else{
			resultDto.setResultMessage("IsReview为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		coursesSeries.setCreationTime(new Date());
		if(qo.getIsSeriesLesson()!=null) {
			coursesSeries.setIsSeriesLesson(qo.getIsSeriesLesson());
		}
		if(qo.getUploader()!=null) {
			TeachersQo teachersQo=new TeachersQo();

			teachersQo.setSeachOrganization(true);
			teachersQo.setSeachUser(true);
			teachersQo.setTeacherId(qo.getUploader());
			Teacher teacher=teacherDao.queryUnique(teachersQo);
			if(teacher.getCoursesNum()==null) {
				teacher.setCoursesNum(0);
			}
			teacher.setCoursesNum(teacher.getCoursesNum()+1);
			coursesSeries.setUploader(teacher);
			teacherDao.update(teacher);
			coursesSeries.setUploader(teacher);
		}else {
			resultDto.setResultMessage("上传人不能为空！");
			resultDto.setSuccess(false);
			return resultDto;
		}
		List<String> sList=new ArrayList<String>();
		sList=qo.getImgList();
		for (int i=0;i<sList.size();i++) {
			CoursePicture coursePicture=new CoursePicture();
			coursePicture.setCoursesSeries(coursesSeries);
			if(qo.getImgList()!=null) {
				coursePicture.setImageUrl(qo.getImgList().get(i));
			}
			coursePicture.setSerialNumber(i+1);
			coursePictureDao.save(coursePicture);;
		}
		coursesSeriesDao.save(coursesSeries);
		resultDto.setSuccess(true);
		resultDto.setUniqueResult(coursesSeries);
		return resultDto;
	}
	@Override
	public ServiceResultDto findpmodify(CoursesSeriesQo coursesSeriesQo) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		CoursesSeriesQo cou =new CoursesSeriesQo();
		cou.setCoursesSeriesId(coursesSeriesQo.getId());
		CoursesSeries series=coursesSeriesDao.queryUnique(cou);
		if(coursesSeriesQo==null) {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		//课程标题
		if(StringUtils.isNotBlank(coursesSeriesQo.getTotalTitle())){
			series.setTotalTitle(coursesSeriesQo.getTotalTitle());
		}else {
			resultDto.setResultMessage("课程标题为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(StringUtils.isNotBlank(coursesSeriesQo.getCourseSecondaryTitle())) {
			series.setCourseSecondaryTitle(coursesSeriesQo.getCourseSecondaryTitle());
		}
		if(StringUtils.isNotBlank(coursesSeriesQo.getCover())) {
			series.setCover(coursesSeriesQo.getCover());
		}
		if(coursesSeriesQo.getCourseShare()!=null) {
			series.setCourseShare(coursesSeriesQo.getCourseShare());
		}
		if(coursesSeriesQo.getCourseTypeOne()!=null) {
			series.setCourseTypeOne(coursesSeriesQo.getCourseTypeOne());
		}
		if(coursesSeriesQo.getCourseTypeTwo()!=null) {
			series.setCourseTypeTwo(coursesSeriesQo.getCourseTypeTwo());
		}
		if(coursesSeriesQo.getIsPrice()==1 || coursesSeriesQo.getIsPrice()==2) {
			series.setIsPrice(coursesSeriesQo.getIsPrice());
//			if(coursesSeriesQo.getIsPrice()==2) {
//				if(coursesSeriesQo.getPrice()!=null) {
//					series.setPrice(coursesSeriesQo.getPrice());
//				}else {
//					resultDto.setResultMessage("请输入费用！");
//					resultDto.setSuccess(false);
//					return resultDto;
//				}
//
//			}
		}
		if(StringUtils.isNotBlank(coursesSeriesQo.getTotalDetails())) {
			series.setTotalDetails(coursesSeriesQo.getTotalDetails());
		}
		coursesSeriesDao.update(series);
		resultDto.setSuccess(true);
		return resultDto;
	}

}
